package com.atguigu.myssm.io;

import javafx.beans.property.Property;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ClassPathXmlApplicationContext implements beanFactory{

    private Map<String,Object> beanMap = new HashMap<>();

    public ClassPathXmlApplicationContext(){
        this("applicationContext.xml");
    }

    public ClassPathXmlApplicationContext(String path){
        try{
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = factory.newDocumentBuilder();
            Document document = documentBuilder.parse(inputStream);
            NodeList nodelist = document.getElementsByTagName("bean");
            for( int i = 0;i<nodelist.getLength();i++){
                Node node =nodelist.item(i);
                if(node.getNodeType()==Node.ELEMENT_NODE){
                    Element element = (Element) node;
                    String id = element.getAttribute("id");
                    String beanclass = element.getAttribute("class");
                    Class clazz = Class.forName(beanclass);
                    Object beanObject = clazz.newInstance();

//                    Method setServletContextMethod = clazz.getDeclaredMethod("setServletContext", ServletContext.class);
//                    setServletContextMethod.invoke(beanObject , this.getServletContext());
                    beanMap.put(id,beanObject);
                }
            }
            //
            for( int i = 0;i<nodelist.getLength();i++) {
                Node node = nodelist.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    String id = element.getAttribute("id");
                    NodeList beanchildList = element.getChildNodes();
                    for(int j=0;j<beanchildList.getLength();j++){
                        Node beanChildNode =  beanchildList.item(j);
                        if(beanChildNode.getNodeType()==Node.ELEMENT_NODE&&"property".equals(beanChildNode.getNodeName())){
                            Element element1 = (Element) beanChildNode;
                            String Propertyname =  element1.getAttribute("name");
                            String Propertyref =  element1.getAttribute("ref");
                            Object refObject = beanMap.get(Propertyref);
                            Object beanObject = beanMap.get(id);
                            Field propertyField = beanObject.getClass().getDeclaredField(Propertyname);
                            propertyField.setAccessible(true);
                            propertyField.set(beanObject,refObject);



                        }
                    }
                    //System.out.println(beanchildList.getLength());



                }
            }


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public Object getBean(String id) {
        return beanMap.get(id);
    }
}
